package com.lanyan.system.controller.system;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.lanyan.common.utils.bean.BeanUtils;
import com.lanyan.system.service.IAccountService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lanyan.common.annotation.Log;
import com.lanyan.common.core.controller.BaseController;
import com.lanyan.common.core.domain.AjaxResult;
import com.lanyan.common.enums.BusinessType;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import com.lanyan.system.domain.Inform;
import com.lanyan.system.service.IInformService;
import com.lanyan.common.utils.poi.ExcelUtil;
import com.lanyan.common.core.page.TableDataInfo;

/**
 * 消息通知Controller
 *
 * @author lanyan
 * @date 2024-11-26
 */
@RestController
@RequestMapping("/system/inform")
public class InformController extends BaseController
{
    @Autowired
    private IInformService informService;
    @Autowired
    private IAccountService accountService;

    /**
     * 查询消息通知列表
     */
    @PreAuthorize("@ss.hasPermi('system:inform:list')")
    @GetMapping("/list")
    @ApiImplicitParams({
          @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
          @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
          @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "accountId", value = "所属用户", dataType = "Long", dataTypeClass = Long.class),
          @ApiImplicitParam(name = "title", value = "标题", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "content", value = "内容", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "toId", value = "来源id", dataType = "Long", dataTypeClass = Long.class),
          @ApiImplicitParam(name = "pageNum", value = "第几页", dataType = "int", dataTypeClass = int.class),
          @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", dataTypeClass = int.class),

    })
    public TableDataInfo list(Inform inform)
    {
        startPage();
        List<Inform> list = informService.selectInformList(inform);
        for (Inform item : list) {
            item.setAccount(accountService.getById(item.getAccountId()));
        }
        return getDataTable(list);
    }

    /**
     * 导出消息通知列表
     */
    @PreAuthorize("@ss.hasPermi('system:inform:export')")
    @Log(title = "消息通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Inform inform)
    {
        List<Inform> list = informService.selectInformList(inform);
        ExcelUtil<Inform> util = new ExcelUtil<Inform>(Inform.class);
        util.exportExcel(response, list, "消息通知数据");
    }

    /**
     * 获取消息通知详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:inform:query')")
    @GetMapping(value = "/{informId}")
    public AjaxResult getInfo(@PathVariable("informId") Long informId)
    {
        return success(informService.selectInformByInformId(informId));
    }

    /**
     * 新增消息通知
     */
    @PreAuthorize("@ss.hasPermi('system:inform:add')")
    @Log(title = "消息通知", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "accountId", value = "所属用户", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "title", value = "标题", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "content", value = "内容", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "toId", value = "来源id", dataType = "Long", dataTypeClass = Long.class),
    })
    public AjaxResult add(@RequestBody Inform inform)
    {
        if (inform.getAccountIds() != null && inform.getAccountIds().size() != 0){
            List<Inform> all = new ArrayList<>();
            for (Long accountId : inform.getAccountIds()) {
                Inform item = new Inform();
                BeanUtils.copyBeanProp(item,inform);
                item.setAccountId(accountId);
                all.add(item);
            }
            informService.saveBatch(all);
        }
        return success();
    }

    /**
     * 修改消息通知
     */
    @PreAuthorize("@ss.hasPermi('system:inform:edit')")
    @Log(title = "消息通知", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "accountId", value = "所属用户", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "title", value = "标题", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "content", value = "内容", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "toId", value = "来源id", dataType = "Long", dataTypeClass = Long.class),
    })
    public AjaxResult edit(@RequestBody Inform inform)
    {
        return toAjax(informService.updateInform(inform));
    }

    /**
     * 删除消息通知
     */
    @PreAuthorize("@ss.hasPermi('system:inform:remove')")
    @Log(title = "消息通知", businessType = BusinessType.DELETE)
	@DeleteMapping("/{informIds}")
    public AjaxResult remove(@PathVariable Long[] informIds)
    {
        return toAjax(informService.deleteInformByInformIds(informIds));
    }
}
